﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BUS;
using Entity;

public partial class Admin_QuanLyLoaiSP : System.Web.UI.Page
{
    DataTable dtLoaiSP;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
            Response.Redirect("../Default.aspx");
        if (Boolean.Parse(Session["QuanLy"].ToString()) == false)
            Response.Redirect("../Default.aspx");
        if (!IsPostBack) 
        {
            bindgrid();
        }
    }
    protected void bindgrid()
    {
        GridView1.DataSource = dtLoaiSP = QuanLyLoaiSPBUS.LayThongTinLoaiSP();
        GridView1.DataBind();
    }
    private void gotoAnchorScript(string anchor)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#" + anchor + "';", true);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        LoaiSanPham loaisp = new LoaiSanPham();
        loaisp.maloaisp = row.Cells[0].Text;
        loaisp.tenloaisp = ((TextBox)row.Cells[1].FindControl("TextBox1")).Text.Trim();
        QuanLyLoaiSPBUS.SuaLoaiSP(loaisp);
        GridView1.EditIndex = -1;
        bindgrid();
        gotoAnchorScript("list");
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindgrid();
        gotoAnchorScript("list");
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        QuanLyLoaiSPBUS.XoaLoaiSP(GridView1.Rows[e.RowIndex].Cells[0].Text);
        bindgrid();
        gotoAnchorScript("list");
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bindgrid();
        gotoAnchorScript("list");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bindgrid();
        gotoAnchorScript("list");
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        tblThem.Visible = true;
        btnThem.Visible = false;
        gotoAnchorScript("form");
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        LoaiSanPham loaisp = new LoaiSanPham();
        loaisp.maloaisp = txtMaLoaiSP.Text.Trim();
        loaisp.tenloaisp = txtTenLoai.Text.Trim();
        QuanLyLoaiSPBUS.ThemLoaiSP(loaisp);
        Response.Redirect("QuanLyLoaiSP.aspx#list");
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("QuanLyLoaiSP.aspx#list");
    }
}
